package backend;

import java.util.Map;
import java.util.TreeMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * @author Santiago Demian
 * 
 */
public class Catalog {
	/**
	 * Don't let anyone instantiate this class.
	 */
	private Catalog() {
		
	}

	public static Map<String,Category> getCategoryList(int language_id) throws Exception {
		Map<String,Category> resp=new TreeMap<String,Category>();
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy");
		String[] arg={"language_id="+language_id};
		
			Document dom = Connection.connect("GetCategoryList", "GET", arg);
			NodeList nodes = dom.getElementsByTagName("category");
			//System.out.println(nodes.getLength());
			for (int i = 0; i < nodes.getLength(); i++) {
				Element e = (Element) nodes.item(i);
				int id = new Integer(e.getAttribute("id"));
				String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
				String code= ((e.getElementsByTagName("code")).item(0)).getTextContent();
				Category aCateg= new Category(id,name,code);
				resp.put(name,aCateg);
				
			}
		
		return resp;
	}

	public static Map<String,Category> getSubcategoryList(int language_id, int category_id) throws Exception{
			Map<String,Category> resp=new TreeMap<String,Category>();
			Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy");
			String[] arg={"language_id="+language_id,"category_id="+category_id};
			
			Document dom = Connection.connect("GetSubcategoryList", "GET", arg);
			NodeList nodes = dom.getElementsByTagName("subcategory");
			
			for (int i = 0; i < nodes.getLength(); i++) {
				Element e = (Element) nodes.item(i);
				int id = new Integer(e.getAttribute("id"));
				String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
				String code= ((e.getElementsByTagName("code")).item(0)).getTextContent();
				Category aCateg= new Category(id,name,code);
				resp.put(name,aCateg);
				
			}
			
			return resp;
	}

      	public static Map<Integer,Product> getProductListByCategory(int language_id,int category_id) throws Exception{
		Map<Integer,Product> resp=new TreeMap<Integer,Product>();
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy");
		String[] arg={"language_id="+language_id,"category_id="+category_id};
		Document dom = Connection.connect("GetProductListByCategory", "GET", arg);
		NodeList nodes = dom.getElementsByTagName("product");
		
		for (int i = 0; i < nodes.getLength(); i++) {
			Element e = (Element) nodes.item(i);
			int id = new Integer(e.getAttribute("id"));
			String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
			int cat_id = (new Integer(((e.getElementsByTagName("category_id")).item(0)).getTextContent())).intValue();
			int sub_id = (new Integer(((e.getElementsByTagName("subcategory_id")).item(0)).getTextContent())).intValue();
			double rank= (new Double(((e.getElementsByTagName("sales_rank")).item(0)).getTextContent())).doubleValue();
			double price=(new Double(((e.getElementsByTagName("price")).item(0)).getTextContent())).doubleValue();
			String image_url=((e.getElementsByTagName("image_url")).item(0)).getTextContent();
			Product aProd= new Product(id,cat_id,sub_id,name,rank,price,image_url,null);
			resp.put(id,aProd);
		}
		
		return resp;
	}

	public static Map<Integer,Product> getProductListBySubcategory(int language_id,int category_id, int subcategory_id) throws Exception {
		Map<Integer,Product> resp=new TreeMap<Integer,Product>();
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy");
		String[] arg={"language_id="+language_id,"category_id="+category_id,"subcategory_id="+subcategory_id};
		Document dom = Connection.connect("GetProductListBySubcategory", "GET", arg);
		NodeList nodes = dom.getElementsByTagName("product");
			
		for (int i = 0; i < nodes.getLength(); i++) {
			Element e = (Element) nodes.item(i);
			int id = new Integer(e.getAttribute("id"));
			String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
			int cat_id = (new Integer(((e.getElementsByTagName("category_id")).item(0)).getTextContent())).intValue();
			int sub_id = (new Integer(((e.getElementsByTagName("subcategory_id")).item(0)).getTextContent())).intValue();
			double rank= (new Double(((e.getElementsByTagName("sales_rank")).item(0)).getTextContent())).doubleValue();
			double price=(new Double(((e.getElementsByTagName("price")).item(0)).getTextContent())).doubleValue();
			String image_url=((e.getElementsByTagName("image_url")).item(0)).getTextContent();
			Product aProd= new Product(id,cat_id,sub_id,name,rank,price,image_url,null);
			resp.put(id,aProd);
		}
		
		return resp;
	}

	public static Map<Integer,Product> getProductListByName(String criteria) throws Exception {
		Map<Integer,Product> resp=new TreeMap<Integer,Product>();
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy");
		String[] arg={"criteria="+criteria};
		Document dom = Connection.connect("GetProductListByName", "GET", arg);
		NodeList nodes = dom.getElementsByTagName("product");
			
		for (int i = 0; i < nodes.getLength(); i++) {
			Element e = (Element) nodes.item(i);
			int id = new Integer(e.getAttribute("id"));
			String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
			int cat_id = (new Integer(((e.getElementsByTagName("category_id")).item(0)).getTextContent())).intValue();
			int sub_id = (new Integer(((e.getElementsByTagName("subcategory_id")).item(0)).getTextContent())).intValue();
			double rank= (new Double(((e.getElementsByTagName("sales_rank")).item(0)).getTextContent())).doubleValue();
			double price=(new Double(((e.getElementsByTagName("price")).item(0)).getTextContent())).doubleValue();
			String image_url=((e.getElementsByTagName("image_url")).item(0)).getTextContent();
			Product aProd= new Product(id,cat_id,sub_id,name,rank,price,image_url,null);
			resp.put(id,aProd);
		}
		return resp;
	}
}
